package com.woniuxy.microgoldservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microgoldservice.bean.*;

import com.woniuxy.microgoldservice.mapper.GoldAccumulateMapper;
import com.woniuxy.microgoldservice.mapper.GoldPriceMapper;
import com.woniuxy.microgoldservice.mapper.GoldPurchaseMapper;
import com.woniuxy.microgoldservice.service.IGoldAccumulateService;
import com.woniuxy.microgoldservice.service.IGoldpriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * @author hongbin
 * @date 2021-04-07 14:05
 * Description：黄金产品
 */
@RefreshScope
@Api(value = "黄金产品接口")
@Slf4j
//@CrossOrigin
@RequestMapping(value = {"/goldAccumulate","/admin"},produces = "application/json;charset=utf-8")
@RestController
public class GoldAccumulateController {

    @Resource
    private IGoldAccumulateService goldService;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private GoldAccumulateMapper accumulateMapper;

    @Resource
    private IGoldpriceService goldpriceService;

    @Resource
    private IGoldAccumulateService goldAccumulateService;

    /**
     * @Author: hongbin
     * @Description: 添加支持的银行
     * @DateTime: 10:39 2021/4/17
     * @Params: []
     * @Return java.lang.String
     */
    @ApiOperation(value = "添加支持的银行", httpMethod = "POST")
    @PostMapping(value = "/addBank")
    public ResultBean addSupportBank(@RequestBody BankInfo bankInfo) {

        int i = goldService.addSupportBank(bankInfo);

        return ResultBean.builder().code(200).msg("OK").data(i).build();

    }

    /**
     * @Author: hongbin
     * @Description: app展示的黄金信息
     * @DateTime: 15:15 2021/4/17
     * @Params: []
     * @Return java.lang.String
     */
    @ApiOperation(value = "黄金产品详情", httpMethod = "GET")
    @RequestMapping(path = "/selectProduct", method = RequestMethod.GET)
    public ResultBean selectDemonstratingMerchandise() {
        return goldService.selectProduct();
    }


    /**
     * @Author: hongbin
     * @Description: 查询所有的黄金产品
     * @DateTime: 15:16 2021/4/17
     * @Params: [pageInfo]
     * @Return java.lang.String
     */
    @ApiOperation(value = "后台-查询所有产品", httpMethod = "GET")
    @GetMapping(value = "/selectAll")
    public ResultBean selectAll(PageInfometion pageInfo) {
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());
        List<GoldAccumulateInfo> goldAccumulateInfos = goldService.selectAll(pageInfo.getName());
        PageInfo page = new PageInfo(goldAccumulateInfos);
        return ResultBean.builder().code(200).msg("OK").data(page).build();
    }

    /**
     * @Author: hongbin
     * @Description: 添加产品信息
     * @DateTime: 15:16 2021/4/17
     * @Params: [goldAccumulateInfo]
     * @Return java.lang.String
     */
    @ApiOperation(value = "后台-添加产品", httpMethod = "POST")
    @PostMapping("/insert")
    public ResultBean insert(@RequestBody GoldAccumulateInfo goldAccumulateInfo) {
        log.info("添加产品++++》{}", goldAccumulateInfo);
        goldService.insert(goldAccumulateInfo);
        return ResultBean.builder().code(200).msg("OK").build();
    }

    /**
     * @Author: hongbin
     * @Description: 修改产品信息
     * @DateTime: 15:18 2021/4/17
     * @Params: [goldAccumulateInfo]
     * @Return java.lang.String
     */
    @ApiOperation(value = "后台-修改产品", httpMethod = "PUT")
    @PutMapping("/update")
    public ResultBean update(GoldAccumulateInfo goldAccumulateInfo) {
        int insert = goldService.update(goldAccumulateInfo);
        return ResultBean.builder().code(200).msg("OK").data(insert).build();
    }


    /**
     * @Author: hongbin
     * @Description: 批量删除
     * @DateTime: 15:19 2021/4/17
     * @Params: [page]
     * @Return java.lang.String
     */
    @ApiOperation(value = "后台-删除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    public ResultBean delete(@RequestBody PageInfometion page) {
        int delete = goldService.delete(page.getIds());
        return ResultBean.builder().code(200).msg("OK").data(delete).build();
    }

    /**
     * @Author: hongbin
     * @Description: 查询数据接口的名称
     * @DateTime: 15:21 2021/4/17
     * @Params: []
     * @Return java.lang.String
     */
    @GetMapping("/dateTypeName")
    public ResultBean selectGoldDataTypeName() {
        List<GoldDataTypeInfo> allTypeName = goldService.getAllTypeName();
        return ResultBean.builder().code(200).msg("OK").data(allTypeName).build();
    }

    /**
     * @Author: hongbin
     * @Description: 上架商品
     * @DateTime: 15:22 2021/4/17
     * @Params: [pageInfo]
     * @Return java.lang.String
     */
    @PutMapping("/goodsShelves")
    public ResultBean goodsShelves(@RequestBody PageInfometion pageInfo) {
        int i = goldService.goodsShelves(pageInfo.getId(), pageInfo.getAccountState());
        return ResultBean.builder().code(200).msg("OK").data(i).build();
    }

    /**
     * @Author: hongbin
     * @Description: 查询风险等级
     * @DateTime: 15:25 2021/4/17
     * @Params: []
     * @Return java.lang.String
     */
    @GetMapping("/goldAPIType")
    public ResultBean getGoldAPITpyes() {
        List<GoldPAITpye> goldPAITpye = goldService.getGoldAPITpyes();
        List<RiskGrade> riskGrade = goldService.getRiskGrade();
        HashMap<String, Object> map = new HashMap<>();
        map.put("goldAPIType", goldPAITpye);
        map.put("riskGrade", riskGrade);
        return ResultBean.builder().code(200).msg("OK").data(map).build();
    }

    /**
     * @Author: hongbin
     * @Description: 有问题
     * @DateTime: 15:26 2021/4/17
     * @Params: []
     * @Return java.lang.String
     */
    @GetMapping("/riskGrade")
    public ResultBean getRiskGrade() {
        List<RiskGrade> riskGrade = goldService.getRiskGrade();
        return ResultBean.builder().code(200).msg("OK").data(riskGrade).build();
    }


    /**
     * @Author: hongbin
     * @Description: 根据id查询某一条产品信息
     * @DateTime: 15:28 2021/4/17
     * @Params: [id]
     * @Return java.lang.String
     */
    @GetMapping("/selectOne")
    public String selectById(@RequestParam("id") Long id) {
        if (id == null) {
            return JSON.toJSONString(ResultBean.builder().code(505).msg("FALSE").data(new GoldAccumulateInfo()).build());
        }
        GoldAccumulateInfo goldAccumulateInfo = goldService.selectOne(id);
        return JSON.toJSONString(ResultBean.builder().code(200).msg("OK").data(goldAccumulateInfo).build());
    }

    /**
     * @Author: hongbin
     * @Description: 查询回收站中的数据
     * @DateTime: 15:28 2021/4/17
     * @Params: [page]
     * @Return java.lang.String
     */
    @GetMapping("/deleteData")
    public String selectDeleteData(PageInfometion page) {

        PageHelper.startPage(page.getPage(), page.getLimit());
        List<GoldAccumulateInfo> goldAccumulateInfos = goldService.selectDeleteData(page.getName());
        PageInfo pages = new PageInfo(goldAccumulateInfos);
        return JSON.toJSONString(ResultBean.builder().code(200).msg("OK").data(pages).build());
    }

    /**
     * @Author: hongbin
     * @Description: 恢复数据
     * @DateTime: 15:28 2021/4/17
     * @Params: [id]
     * @Return java.lang.String
     */
    @PostMapping("/recover")
    public String recoverData(Long id) {
        int i = goldService.recoverData(id);
        return JSON.toJSONString(ResultBean.builder().code(200).msg("OK").data(i).build());
    }

    /**
     * @Author: hongbin
     * @Description: 彻底删除数据
     * @DateTime: 15:29 2021/4/17
     * @Params: [ids]
     * @Return java.lang.String
     */
    @PostMapping("/completely")
    public String completelyDelete(@RequestBody Long[] ids) {
        int i = goldService.completelyDelete(ids);
        return JSON.toJSONString(ResultBean.builder().code(200).msg("OK").data(i).build());
    }

    @Resource
    private GoldPriceMapper goldPriceMapper;

    @Resource
    private GoldAccumulateMapper goldAccumulateMapper;

    @Resource
    private GoldPurchaseMapper goldPurchaseMapper;

    @GetMapping("/test")
    public String test() {
        goldpriceService.setEarnings();

        return "OK";
    }
}
